import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  static const platform = MethodChannel('my.test.channel');

  Future<void> _getNativeMessage() async {
    try {
      final message = await platform.invokeMethod('getMessage');
      print('收到原生返回: $message');
    } catch (e) {
      print('调用原生失败: $e');
    }
  }

  Future<void> _sendLog() async {
    await platform.invokeMethod('printLog', {'msg': 'Flutter 调用了 iOS 原生'});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('MethodChannel iOS 测试')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: _getNativeMessage,
                child: const Text('获取原生消息'),
              ),
              ElevatedButton(onPressed: _sendLog, child: const Text('发送日志到原生')),
            ],
          ),
        ),
      ),
    );
  }
}
